package com.jcgyl.export.controller;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.entity.Tbuploadinfo;
import com.jcgyl.export.mapper.TbuploadinfoMapper;
import com.jcgyl.export.model.BaseResponseModel;
import com.jcgyl.export.model.GenerateParameter;
import com.jcgyl.export.mq.MessageSender;


@RestController
@RequestMapping("/order/inspect")
public class InspectController {
	@Autowired
	private TbuploadinfoMapper tbuploadinfoMapper;
	@Autowired
	private MessageSender messageSender;
	@PostMapping("/send")
	public BaseResponseModel Send(@RequestBody GenerateParameter parameter) {
		Tbsysuser user=(Tbsysuser)SecurityUtils.getSubject().getSession().getAttribute("user");
		System.out.println("发送的用户"+user.getUname());
		BaseResponseModel model = new BaseResponseModel();
		int uploadId=parameter.getUploadId();
		System.out.println("上传者ID"+uploadId); 
		try {
			Tbuploadinfo upload = tbuploadinfoMapper.selectByPrimaryKey(uploadId);
			String file_path=upload.getFilePath();
			String inspect_message=readFileAsString(file_path);
			messageSender.send(inspect_message);
		} catch (Exception e) {
			model.setIsError(true);
			model.setMessage(e.getMessage());
		}
		return model;
	}
	//把文件转化字符串
	private String readFileAsString(String filePath) throws IOException {
        StringBuffer fileData = new StringBuffer();
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }
}
